<?php

function api_ftp_connect() {
    $config = api_file_info_config();    
    //address of ftp server.
    foreach ($config as $con) { 
        $ftp_user_name = $con['user']; // Username
        $ftp_user_pass = $con['pass'];   // Password
        $conn_id = ftp_connect($con['server']);
        //$conn_id = ftp_ssl_connect($config[0]['server']);
        $login_result = ftp_login($conn_id , $ftp_user_name, $ftp_user_pass);   // login with username and password, or give invalid user message\        
         
        if ((!$conn_id) || (!$login_result)) {  // check connection             
            return false;
        }
        else{
            return $conn_id;
        }
    } 
    return false;
}


function file_extension($filename) {
    return end(explode(".", $filename));
}

function checkMimeTypeImage($typeMime) {
    $arrMime = array('image/gif', 'image/jpe', 'image/jpeg', 'image/png', 'image/bmp');
    return in_array($typeMime, $arrMime);
}


function api_file_save_data($file, $filename, $uid = '', $manage = TRUE, $mode = FTP_BINARY) {  
    $fileInfo = getimagesize($file);           
    if (!isset($fileInfo[0]) || ( isset($fileInfo[0]) && $fileInfo[0] <= 0)) {       
        return NULL;
    }
    if (!isset($fileInfo['mime'])) {       
        return NULL;
    }
    if (isset($fileInfo['mime']) && checkMimeTypeImage($fileInfo['mime'])) {            
        $filename = time() . str_random(5) . '.' . file_extension($filename);
        $config = api_file_info_config();        
        $conn_id = api_ftp_connect();      
        $pathFile = $uid . '/' . $filename; 
        
        @ftp_mkdir($conn_id, $uid);
        @ftp_chmod($conn_id, 0777, $uid);       
        
                
        if (ftp_put($conn_id, $pathFile, $file, $mode)) {         
            $fs = new stdClass();
            $fs->uid = $uid;
            $fs->uri = $pathFile;
            $fs->filename = $filename;
            $fs->serverip = $config[0]['server'];
            $fs->filemime = 'image/jpeg';
            $fs->filesize = filesize($file);
            $fs->url = $config[0]['basic_url'] . '?q=' . $fs->uri . '/' .  'album_thumb';
            $fs->url_custom = $config[0]['url'] . $fs->uri ;
            $fs->url_basic = $config[0]['url'] . $fs->uri ;
            $fs->thubm = $config[0]['basic_url'] . 'stores/' . $fs->uid  .  '/album_thumb/' . $fs->filename;
            $fs->status = 1;
            $fs->timestamp = time();
            $fs->width = $fileInfo[0];
            $fs->height = $fileInfo[1]; 
            if ($manage == TRUE){
				$fs->fid = mdInsertImages( $fs );
                //drupal_write_record('tbnd_file_server', $fs);
			}

            @ftp_chmod($conn_id, 0777, $pathFile);
            ftp_close($conn_id);
           
            return $fs;
        }else {            
            return NULL;
        }
    }
}


function api_file_delete($file) {
    if (!isset($file->uri)) {
        $file = api_file_load($file->fid);
    }    
    /* old code
    $conn_id = api_ftp_connect();        
    $r = @ftp_delete($conn_id, $file->uri); 
     old code --*/
    
    $a_conn_id = api_ftp_connect();        
    foreach ($a_conn_id as $conn_id) {
        $r = @ftp_delete($conn_id, $file->uri); 
    }
   
    /*$arr = _catalog_file();   
    foreach ($arr as $key => $val) {
        $r = @ftp_delete($conn_id, $file->uid . '/' . $key . '/' . $file->filename);
    }
    */
    
    db_delete('tb_file_server')->condition('fid', $file->fid)->execute();
    //ftp_close($conn_id);
    return $r;
}

function api_file_load($fid) {	
    $config = api_file_info_config();
    $query = db_select('tbnd_file_server', 'f')
            ->fields('f')
            ->condition('fid', $fid)
            ->execute();
    if ($query->rowCount() > 0) {
        $file = $query->fetchObject();
        $file->url = str_replace($config[0]['path_unix'], '', $config[0]['url'] . $file->uri);
        $file->url_custom =  $config[0]['basic_url'] . '/stores/' . $file->uid  .  '/' . $file->filename;
        $file->thubm = $config[0]['basic_url'] . 'stores/' . $file->uid  .  '/album_thumb/' . $file->filename;
        $file->url_basic = $file->url;
        return $file;
    }else
        return NULL;
}


